package bachelor {
	import CustomEvent.CustomEvent;

	import bachelor.view.GUI;

	import fiducialTuioAS3.tuio.TUIO;

	import gs.OverwriteManager;
	import gs.TweenLite;
	import gs.easing.Cubic;

	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;

	[SWF(backgroundColor = "0x555555", width="1024", height="786", frameRate="30")]

	/**
	 * @author Admin
	 */
	public class Main extends MovieClip {


//////////////////////////////////////////////////////////////////////
/////// VARIABLES ////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////				
		
		private var _gui : GUI;
		private var _dragPoint : Point;
		
		
//////////////////////////////////////////////////////////////////////
/////// CONSTRUCTOR //////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
		public function Main() 
		{
			
//			trace("Main -> Constructor init...");
			
            //--------connect to TUIO-------------------------------------------
            TUIO.init(this,'localhost',3000,'',false);
//            trace("MyTouchApp Initialized");

            //--------TweenLite OverwriteManager -------------------------------
			OverwriteManager.init();
			
            //--------3D -------------------------------------------------------
			_dragPoint = new Point (512, 384);
			this.transform.perspectiveProjection.fieldOfView = 85; //170

			// -------GUI ------------------------------------------------------
			init();
			
			

		}
		
		
		
//////////////////////////////////////////////////////////////////////
/////// METHODS //////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////


/////// INIT /////////////////////////////////////////////////////////		
		private function init() : void
		{
//			trace("Main -> init() ...");
			
			var backgroundLoopSound:Sound = new Sound(new URLRequest("bin/sounds/backgroundLoopSound.mp3"));
			var trans1:SoundTransform = new SoundTransform(0.4);
			var channel1:SoundChannel = new SoundChannel();
			channel1 = backgroundLoopSound.play(0, 999999, trans1);
			

			_gui = new GUI();
			addChild(_gui);
			_gui.addEventListener(GUI.COUNTRY_FIDUCIAL_MOVED, onCountryFiducialMovedListener);
			_gui.addEventListener(GUI.COUNTRY_FIDUCIAL_ADDED, onCountryFiducialAddedListener);
		}
		
		
/////// oEF ////////////////////////////////////////////////////////////////////		
		
		private function onCountryFiducialAddedListener(evt:CustomEvent) : void 
		{
			var xPos : int = Math.round(evt.data[0] - 512) * 3; 
			var yPos : int = Math.round(evt.data[1] - 384) * 3;
			TweenLite.to(_dragPoint, 0.5, {x: xPos, y: yPos, ease: Cubic.easeOut, overwrite: 2, onUpdate: setProjectionCenter});
		}
		

		
		private function onCountryFiducialMovedListener(evt:CustomEvent) : void 
		{
			var xPos : int = Math.round(evt.data[0] - 512) * 3 - 500; 
			var yPos : int = Math.round(evt.data[1] - 384) * 3 - 500;
			TweenLite.to(_dragPoint, 4, {x: xPos, y: yPos, ease: Cubic.easeOut, overwrite: 2, onUpdate: setProjectionCenter});
		}

		private function setProjectionCenter() : void 
		{
			root.transform.perspectiveProjection.projectionCenter = _dragPoint;		
		}
		
		
		
		
		
		
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
	}
}
